home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-2.iso / Files II / Prog / M / MacPerl 4.13 source.sit / Perl Source ƒ / MacPerl / MPWindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-04  |  28.5 KB  |  1,196 lines  |  [TEXT/MPS ]

  1. /*********************************************************************
  2. Project    :    MacPerl            -    Real Perl Application
  3. File        :    MPWindow.c        -
  4. Author    :    Matthias Neeracher
  5.  
  6. A lot of this code is borrowed from 7Edit written by
  7. Apple Developer Support UK
  8.  
  9. Language    :    MPW C
  10.  
  11. $Log: MPWindow.c,v $
  12. Revision 1.2  1994/05/04  02:52:40  neeri
  13. Inline Input.
  14.  
  15. Revision 1.1  1994/02/27  23:02:22  neeri
  16. Initial revision
  17.  
  18. Revision 0.4  1993/08/17  00:00:00  neeri
  19. A little more defensiveness
  20.  
  21. Revision 0.3  1993/08/06  00:00:00  neeri
  22. Draw pretty icons
  23.  
  24. Revision 0.2  1993/05/30  00:00:00  neeri
  25. Support Console Windows
  26.  
  27. Revision 0.1  1993/05/29  00:00:00  neeri
  28. Compiles correctly
  29.  
  30. *********************************************************************/
  31.  
  32. #include <Resources.h>
  33. #include <Scrap.h>
  34. #include <Packages.h>
  35. #include <PLStringFuncs.h>
  36. #include <Icons.h>
  37. #include "MPWindow.h"
  38. #include "MPConsole.h"
  39. #include "MPEditions.h"
  40.  
  41. #define    kControlInvisible    0
  42. #define    kControlVisible      0xFF
  43. #define    kScrollbarWidth        16
  44. #define    kScrollbarAdjust        (kScrollbarWidth - 1)
  45. #define    kScrollTweek           2
  46. #define    kTextOffset                5
  47. #define    kButtonScroll          10
  48.  
  49. #define    kMaxPages              1000 /* Assumes pages > 32 pixels high */
  50.  
  51. #define    kHOffset                        20   /* Stagger window offsets */
  52. #define    kVOffset                        20
  53.  
  54. #define    kTBarHeight                20
  55. #define    kMBarHeight                20
  56.  
  57. typedef short PageEndsArray[kMaxPages];
  58.  
  59. #pragma segment Window
  60.  
  61. pascal DPtr DPtrFromWindowPtr(WindowPtr w)
  62. {
  63.     if (w)
  64.         return((DPtr)GetWRefCon(w));
  65.     else
  66.         return(nil);
  67. } /* DPtrFromWindowPtr */
  68.  
  69. #pragma segment main
  70.  
  71. /*
  72.   Scroll the TERec around to match up to the potentially updated scrollbar
  73.   values. This is really useful when the window resizes such that the
  74.   scrollbars become inactive and the TERec had been previously scrolled.
  75. */
  76. pascal void AdjustTE(DPtr theDoc)
  77. {
  78.     short    h;
  79.     short    v;
  80.     TEHandle myText;
  81.  
  82.     myText = theDoc->theText;
  83.     h =
  84.         ((*myText)->viewRect.left - (*myText)->destRect.left) -
  85.         GetCtlValue(theDoc->hScrollBar) + kTextOffset;
  86.  
  87.     v =
  88.         ((*myText)->viewRect.top - (*myText)->destRect.top) -
  89.         GetCtlValue(theDoc->vScrollBar) + kTextOffset;
  90.  
  91.     if (h || v) {
  92.         TEScroll(h, v, theDoc->theText);
  93.         DrawPageExtras(theDoc);
  94.     }
  95. }  /* AdjustTE */
  96.  
  97.  
  98. /*Calculate the new control maximum value and current value, whether it is the horizontal or
  99. vertical scrollbar. The vertical max is calculated by comparing the number of lines to the
  100. vertical size of the viewRect. The horizontal max is calculated by comparing the maximum document
  101. width to the width of the viewRect. The current values are set by comparing the offset between
  102. the view and destination rects. If necessary and we canRedraw, have the control be re-drawn by
  103. calling ShowControl.*/
  104.  
  105. /*TEStyleSample-vertical max originally used line by line calculations-lineheight was a
  106. constant value so it was easy to figure out what the range should be and pin the value
  107. within range. Now we need to use max and min values in pixels rather than in nlines*/
  108.  
  109. #pragma segment main
  110.  
  111. pascal void AdjustHV(
  112.     Boolean        isVert,
  113.     ControlHandle  control,
  114.     DPtr           theDoc,
  115.     Boolean        canRedraw)
  116. {
  117.     TEHandle    docTE;
  118.     short       value;
  119.     short           max;
  120.     short           oldValue;
  121.     short           oldMax;
  122.     Rect             sizeRect;
  123.  
  124.     sizeRect = theDoc->pageSize;
  125.     docTE    = theDoc->theText;
  126.  
  127.     oldValue = GetCtlValue(control);
  128.     oldMax   = GetCtlMax(control);
  129.     if (isVert)
  130.         max = (*docTE)->nLines*(*docTE)->lineHeight - ((*docTE)->viewRect.bottom - (*docTE)->viewRect.top);
  131.     else
  132.         max = sizeRect.right - ((*docTE)->viewRect.right - (*docTE)->viewRect.left);
  133.  
  134.     max += kTextOffset + kTextOffset; /* Allow over scroll by kTextOffset */
  135.  
  136.     if (max < 0)
  137.         max = 0; /* check for negative values */
  138.  
  139.     SetCtlMax(control, max);
  140.  
  141.     if (isVert)
  142.         value = (*docTE)->viewRect.top - (*docTE)->destRect.top;
  143.     else
  144.         value = (*docTE)->viewRect.left - (*docTE)->destRect.left;
  145.  
  146.     value += kTextOffset;
  147.  
  148.     if (value < 0) {
  149.         TEScroll(isVert ? 0 : value, isVert ? value : 0, docTE);
  150.         DrawPageExtras(theDoc);
  151.  
  152.         value = 0;
  153.     } else if (value > max) {
  154.         TEScroll(isVert ? 0 : value-max, isVert ? value-max : 0, docTE);
  155.         DrawPageExtras(theDoc);
  156.         
  157.         value = max; /* pin the value to within range */
  158.     }
  159.     SetCtlValue(control, value);
  160.     if (canRedraw && ((max != oldMax) || (value != oldValue)))
  161.         ShowControl(control); /* check to see if the control can be re-drawn */
  162. } /* AdjustHV */
  163.  
  164. #pragma segment Main
  165.  
  166. pascal void AdjustScrollValues(DPtr theDoc, Boolean canRedraw)
  167. {
  168.     AdjustHV(true,  theDoc->vScrollBar, theDoc, canRedraw);
  169.     AdjustHV(false, theDoc->hScrollBar, theDoc, canRedraw);
  170. }        /* AdjustScrollValues */
  171.  
  172. pascal void GetTERect(WindowPtr window, Rect  *teRect)
  173. {
  174.      *teRect = window->portRect;
  175.      (*teRect).bottom -= kScrollbarAdjust; /* and for the scrollbars */
  176.      (*teRect).right  -= kScrollbarAdjust;
  177. }         /* GetTERect */
  178.  
  179. /* Re-calculate the position and size of the viewRect and the scrollbars.
  180.   kScrollTweek compensates for off-by-one requirements of the scrollbars
  181.   to have borders coincide with the growbox. */
  182.  
  183. pascal void AdjustScrollSizes(DPtr theDoc)
  184. {
  185.     Rect    teRect;
  186.     Rect    myPortRect;
  187.  
  188.     GetTERect(theDoc->theWindow, &teRect); /*start with teRect*/
  189.     myPortRect = theDoc->theWindow->portRect;
  190.  
  191.     (*(theDoc->theText))->viewRect = teRect;
  192.  
  193.     MoveControl(theDoc->vScrollBar, myPortRect.right - kScrollbarAdjust, -1);
  194.     SizeControl(
  195.         theDoc->vScrollBar,
  196.         kScrollbarWidth,
  197.         (myPortRect.bottom - myPortRect.top) - (kScrollbarAdjust - kScrollTweek));
  198.  
  199.     MoveControl(theDoc->hScrollBar, 31, myPortRect.bottom - kScrollbarAdjust);
  200.     SizeControl(
  201.         theDoc->hScrollBar,
  202.         (myPortRect.right - myPortRect.left) - (kScrollbarAdjust - kScrollTweek + 32),
  203.         kScrollbarWidth);
  204. }        /* AdjustScrollSizes */
  205.  
  206. #pragma segment Window
  207.  
  208. /* Turn off the controls by jamming a zero into their contrlVis fields
  209.   (HideControl erases them and we don't want that). If the controls are to
  210.   be resized as well, call the procedure to do that, then call the procedure
  211.   to adjust the maximum and current values. Finally reset the controls
  212.   to be visible if not in background. */
  213.  
  214. pascal void AdjustScrollbars(DPtr theDoc, Boolean  needsResize)
  215. {
  216.     Boolean    background = gInBackground || theDoc->theWindow != gActiveWindow;
  217.     
  218.     (*(theDoc->vScrollBar))->contrlVis = kControlInvisible; /* turn them off */
  219.     (*(theDoc->hScrollBar))->contrlVis = kControlInvisible;
  220.  
  221.     if (needsResize) /* move and size if needed */
  222.         AdjustScrollSizes(theDoc);
  223.  
  224.     AdjustScrollValues(theDoc, !needsResize && !background); /* fool with max and current value */
  225.  
  226.      /* Now, restore visibility in case we never had to ShowControl during adjustment */
  227.  
  228.     if (!background) {
  229.         (*(theDoc->vScrollBar))->contrlVis = kControlVisible; /* turn them on */
  230.         (*(theDoc->hScrollBar))->contrlVis = kControlVisible;
  231.     } else { /* make sure they stay invisible */
  232.         if ((*(theDoc->vScrollBar))->contrlVis)
  233.             HideControl(theDoc->vScrollBar);
  234.         if ((*(theDoc->vScrollBar))->contrlVis)
  235.             HideControl(theDoc->hScrollBar);
  236.     }
  237. }        /* AdjustScrollbars */
  238.  
  239. #pragma segment Window
  240.  
  241. pascal void GetWinContentRect(WindowPtr theWindow, Rect *r)
  242. {
  243.     *r         = theWindow->portRect;
  244.     r->right  -= kScrollbarAdjust;
  245.     r->bottom -= kScrollbarAdjust;
  246. }  /* GetWinContentRect */
  247.  
  248. #pragma segment Window
  249.  
  250. pascal void InvalidateDocument(DPtr theDoc)
  251. {
  252.     GrafPtr oldPort;
  253.  
  254.     GetPort(&oldPort);
  255.     SetPort(theDoc->theWindow);
  256.     InvalRect(&theDoc->theWindow->portRect);
  257.     SetPort(oldPort);
  258. }
  259.  
  260. /* Called when the window has been resized to fix up the controls and content */
  261.  
  262. pascal void ResizeWindow(DPtr theDoc)
  263. {
  264.     AdjustScrollbars(theDoc, true);
  265.     AdjustTE(theDoc);
  266.     InvalidateDocument(theDoc);
  267. }         /* ResizeWindow */
  268.  
  269. /* Called when the window has been resized to fix up the controls and content */
  270.  
  271. pascal void ResizePageSetupForDocument(DPtr theDoc)
  272. {
  273.     theDoc->pageSize = (*(theDoc->thePrintSetup))->prInfo.rPage;
  274.  
  275.     OffsetRect(&(theDoc->pageSize), -theDoc->pageSize.left, -theDoc->pageSize.top);
  276.  
  277.     (*(theDoc->theText))->destRect.right = (*(theDoc->theText))->destRect.left +
  278.                                                         theDoc->pageSize.right;
  279.  
  280.     TECalText(theDoc->theText);
  281.  
  282.     ResizeWindow(theDoc);
  283. }         /* ResizePageSetupForDocument */
  284.  
  285. #pragma segment Main
  286.  
  287. /* Common algorithm for setting the new value of a control. It returns the actual amount
  288. the value of the control changed. Note the pinning is done for the sake of returning
  289. the amount the control value changed. */
  290.  
  291. pascal void CommonAction(ControlHandle control, short *amount)
  292. {
  293.     short   value;
  294.     short   max;
  295.  
  296.     value   = GetCtlValue(control); /* get current value */
  297.     max     = GetCtlMax(control); /* and max value */
  298.     *amount = value - *amount;
  299.     if (*amount < 0)
  300.           *amount = 0;
  301.     else if (*amount > max)
  302.         *amount = max;
  303.  
  304.     SetCtlValue(control, *amount);
  305.     *amount = value - *amount; /* calculate true change */
  306. }         /* CommonAction */
  307.  
  308. #pragma segment Main
  309.  
  310. /* Determines how much to change the value of the vertical scrollbar by and how
  311.   much to scroll the TE record. */
  312.  
  313. pascal void VActionProc(ControlHandle control, short part)
  314. {
  315.     short           amount;
  316.     WindowPtr       window;
  317.     DPtr            theDoc;
  318.  
  319.      if (part) {
  320.           window = (*control)->contrlOwner;
  321.         theDoc = DPtrFromWindowPtr(window);
  322.         switch (part) {
  323.         case inUpButton:
  324.         case inDownButton:
  325.             amount = 24;
  326.             break;
  327.  
  328.         case inPageUp:
  329.         case inPageDown:
  330.             amount = (*(theDoc->theText))->viewRect.bottom -
  331.                         (*(theDoc->theText))->viewRect.top;
  332.             break;
  333.         }   /* case */
  334.  
  335.         if (part == inDownButton || part == inPageDown)
  336.              amount = -amount; /* reverse direction */
  337.  
  338.         CommonAction(control, &amount);
  339.  
  340.         if (amount) {
  341.             TEScroll(0, amount, theDoc->theText);
  342.             DrawPageExtras(theDoc);
  343.         }
  344.     }     /* if */
  345. }  /* VActionProc */
  346.  
  347. #pragma segment Main
  348.  
  349. /* Determines how much to change the value of the horizontal scrollbar by and how
  350.   much to scroll the TE record. */
  351.  
  352. pascal void HActionProc(ControlHandle control, short part)
  353. {
  354.     short      amount;
  355.     WindowPtr  window;
  356.     DPtr       theDoc;
  357.  
  358.     if (part) {
  359.         window = (*control)->contrlOwner;
  360.         theDoc = DPtrFromWindowPtr(window);
  361.         switch (part) {
  362.         case  inUpButton:
  363.         case  inDownButton:
  364.             amount = kButtonScroll; /* a few pixels */
  365.             break;
  366.         case  inPageUp:
  367.         case  inPageDown:
  368.             amount = (*(theDoc->theText))->viewRect.right -
  369.                         (*(theDoc->theText))->viewRect.left; /* a page */
  370.             break;
  371.         }   /* switch */
  372.         if (part == inDownButton || part == inPageDown)
  373.             amount = - amount; /* reverse direction */
  374.  
  375.         CommonAction(control, &amount);
  376.         if (amount) {
  377.             TEScroll(amount, 0, theDoc->theText);
  378.             DrawPageExtras(theDoc);
  379.         }
  380.     }     /* if */
  381. }         /* HActionProc */
  382.  
  383. /**-----------------------------------------------------------------------
  384.         Name:         ShowSelect
  385.         Purpose:        Scrolls the text selection into view.
  386.     -----------------------------------------------------------------------**/
  387.  
  388. #pragma segment Window
  389.  
  390. pascal void ShowSelect(DPtr theDoc)
  391. {
  392.     if (!theDoc)
  393.         return;
  394.         
  395.      AdjustScrollbars(theDoc, false);
  396.  
  397.     /*
  398.         Let TextEdit do the hard work of keeping the selection visible…
  399.     */
  400.  
  401.     TEAutoView(true, theDoc->theText);
  402.     TESelView(theDoc->theText);
  403.     TEAutoView(false, theDoc->theText);
  404.  
  405.     /*
  406.         Now rematch the text and the scrollbars…
  407.     */
  408.  
  409.     SetCtlValue(
  410.         theDoc->hScrollBar,
  411.         (*(theDoc->theText))->viewRect.left -
  412.         (*(theDoc->theText))->destRect.left + kTextOffset);
  413.  
  414.     SetCtlValue(
  415.         theDoc->vScrollBar,
  416.         (*(theDoc->theText))->viewRect.top -
  417.         (*(theDoc->theText))->destRect.top  + kTextOffset);
  418. }  /* ShowSelect */
  419.  
  420. #pragma segment Window
  421.  
  422. pascal void OffsetWindow(WindowPtr aWindow)
  423. {
  424.     short theWidth;
  425.     short theHeight;
  426.     short theHScreen;
  427.     short theVScreen;
  428.     short xWidth;
  429.     short xHeight;
  430.     short hMax;
  431.     short vMax;
  432.     short wLeft;
  433.     short wTop;
  434.  
  435.     theWidth  = aWindow->portRect.right - aWindow->portRect.left;
  436.     theHeight = aWindow->portRect.bottom - aWindow->portRect.top + kTBarHeight;
  437.  
  438.     theHScreen = qd.screenBits.bounds.right  - qd.screenBits.bounds.left;
  439.     theVScreen = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top;
  440.  
  441.     xWidth  = theHScreen - theWidth;
  442.     xHeight = theVScreen - (theHeight + kMBarHeight);
  443.  
  444.     hMax = (xWidth / kVOffset) + 1;
  445.     vMax = (xHeight / kVOffset) + 1;
  446.  
  447.     gWCount++;
  448.  
  449.     wLeft = (gWCount % hMax) * kVOffset;
  450.     wTop  = ((gWCount % vMax) * kVOffset) + kTBarHeight + kMBarHeight;
  451.  
  452.     MoveWindow(aWindow, wLeft, wTop, false);
  453. }
  454.  
  455.  
  456. /* Returns the update region in local coordinates */
  457.  
  458. pascal void GetLocalUpdateRgn(WindowPtr window, RgnHandle localRgn)
  459. {
  460.     CopyRgn(((WindowPeek)window)->updateRgn, localRgn); /* save old update region */
  461.     OffsetRgn(localRgn, window->portBits.bounds.left, window->portBits.bounds.top); /* convert to local coords */
  462. }          /* GetLocalUpdateRgn */
  463.  
  464. #pragma segment Window
  465.  
  466. pascal void IssueZoomCommand(WindowPtr whichWindow, short whichPart);
  467. pascal void IssueSizeWindow(WindowPtr whichWindow,short newHSize, short newVSize);
  468.  
  469. pascal void MyGrowWindow(WindowPtr w, Point p)
  470. {
  471.     GrafPtr savePort;
  472.     long    theResult;
  473.     Rect    r;
  474.  
  475.     GetPort(&savePort);
  476.     SetPort(w);
  477.     SetRect(&r, 80, 80, qd.screenBits.bounds.right, qd.screenBits.bounds.bottom);
  478.     theResult = GrowWindow(w, p, &r);
  479.     if (theResult)
  480.         IssueSizeWindow(w, LoWord(theResult), HiWord(theResult));
  481.  
  482.     SetPort(savePort);
  483. }
  484.  
  485. #pragma segment Window
  486.  
  487. pascal void DoZoom(WindowPtr w, short c, Point     p)
  488. {
  489.     GrafPtr savePort;
  490.  
  491.      GetPort(&savePort);
  492.     SetPort(w);
  493.      if (TrackBox(w, p, c)) {
  494.         EraseRect(&w->portRect);
  495.         IssueZoomCommand(w, c);
  496.     }
  497. }
  498.  
  499. #pragma segment Window
  500.  
  501. pascal void DoContent(WindowPtr theWindow, EventRecord * theEvent)
  502. {
  503.     short         cntlCode;
  504.     short         part;
  505.     ControlHandle theControl;
  506.     GrafPtr       savePort;
  507.     Boolean       extend;
  508.     DPtr          theDoc;
  509.     short         value;
  510.  
  511.     GetPort(&savePort);
  512.     SetPort(theWindow);
  513.     theDoc = DPtrFromWindowPtr(theWindow);
  514.  
  515.     GlobalToLocal(&theEvent->where);
  516.     cntlCode = FindControl(theEvent->where, theWindow, &theControl);
  517.  
  518.     /*only extend the selection if the shiftkey is down*/
  519.     if (cntlCode == 0) {
  520.           extend = (theEvent->modifiers & shiftKey) != 0;
  521.  
  522.         if (PtInRect(theEvent->where, &(*(theDoc->theText))->viewRect))
  523.             TEClick(theEvent->where, extend, theDoc->theText);
  524.     } else if (cntlCode == inThumb) {
  525.         value = GetCtlValue(theControl);
  526.         part  = TrackControl(theControl, theEvent->where, nil);
  527.         if (part) {
  528.             value -= GetCtlValue(theControl);
  529.             if (value) {
  530.                 if (theControl == theDoc->vScrollBar)
  531.                     TEScroll(0, value, theDoc->theText);
  532.                 else
  533.                     TEScroll(value, 0, theDoc->theText);
  534.                 DrawPageExtras(theDoc);
  535.             }
  536.         } /* if */
  537.     } else
  538.         if (theControl == theDoc->vScrollBar)
  539.             part = TrackControl(theControl, theEvent->where, (ProcPtr)VActionProc);
  540.         else
  541.             part = TrackControl(theControl, theEvent->where, (ProcPtr)HActionProc);
  542.  
  543.     SetPort(savePort);
  544. }
  545.  
  546. #pragma segment Window
  547.  
  548. pascal void AdjustScript(DPtr doc)
  549. {
  550.     ScriptCode    fontScript;
  551.     ScriptCode    keyScript;
  552.  
  553.     fontScript = FontToScript((*doc->theText)->txFont);
  554.     keyScript  = GetSMVariable(smKeyScript);
  555.     
  556.     /* White man's burden: A roman keyboard script fits everywhere. */
  557.     if (keyScript != fontScript && keyScript != smRoman) 
  558.         KeyScript(fontScript);
  559. }
  560.  
  561. #pragma segment Window
  562.  
  563. pascal OSErr DoActivate(WindowPtr theWindow, Boolean   activate)
  564. {
  565.     OSErr err;
  566.     Rect  r;
  567.     DPtr  theDoc;
  568.  
  569.     err = noErr;
  570.  
  571.     if (theWindow) {
  572.         theDoc = DPtrFromWindowPtr(theWindow);
  573.         SetPort(theWindow);
  574.         DrawGrowIcon(theWindow);
  575.         GetWinContentRect(theWindow, &r);
  576.         InvalRect(&r);
  577.         if (activate) {
  578.             gActiveWindow = theWindow;
  579.             
  580.             TEActivate(theDoc->theText);
  581.             ShowControl(theDoc->vScrollBar);
  582.             ShowControl(theDoc->hScrollBar);
  583.             DisableItem(myMenus[editM], undoCommand);
  584.             err = TEFromScrap();
  585.             AdjustScript(theDoc);
  586. #ifndef RUNTIME
  587.             if (theDoc->tsmDoc)
  588.                 ActivateTSMDocument(theDoc->tsmDoc);
  589. #endif
  590.         } else {
  591. #ifndef RUNTIME
  592.             if (theDoc->tsmDoc)
  593.                 DeactivateTSMDocument(theDoc->tsmDoc);
  594. #endif
  595.  
  596.             gActiveWindow = nil;
  597.             
  598.             TEDeactivate(theDoc->theText);
  599.             HideControl(theDoc->vScrollBar);
  600.             HideControl(theDoc->hScrollBar);
  601.             err = ZeroScrap();
  602.             err = TEToScrap();
  603.         }
  604.     }
  605.  
  606.       return err;
  607. }
  608.  
  609. #pragma segment Window
  610.  
  611. pascal void GetPageEnds(
  612.     short          pageHeight,
  613.     TEHandle       theText,
  614.     PageEndsArray  pageBounds,
  615.     short          *nPages)
  616. {
  617.     short  pageBase;      /* total pixel offset of pages so far */
  618.     short  thisLine;
  619.     short  lastLine;
  620.     short  pageSoFar;
  621.     short  thisPage;      /* Current page being calced */
  622.     short  thisLineH;     /* Height of text line */
  623.     short  pageFirstLine; /* Line # of top of page */
  624.  
  625.     pageBase   = 0;
  626.     thisLine   = 1;
  627.     lastLine   = (*theText)->nLines;
  628.  
  629.     thisPage   = 0;
  630.     pageSoFar  = 0;
  631.     while ((thisLine <= lastLine) || (pageSoFar!=0)) {
  632.         pageFirstLine = thisLine;
  633.         thisLineH     = TEGetHeight(thisLine, thisLine, theText);
  634.  
  635.         while ((thisLineH+pageSoFar<pageHeight) && (thisLine <= lastLine)) {
  636.             pageSoFar += thisLineH;
  637.             thisLine++;
  638.             thisLineH = TEGetHeight(thisLine, thisLine, theText);
  639.         }
  640.  
  641.         if (pageSoFar) {
  642.             pageBounds[thisPage] = pageSoFar+pageBase;
  643.             pageBase  = pageBounds[thisPage];
  644.             thisPage++;
  645.             pageSoFar = 0;
  646.         }
  647.  
  648.         /*
  649.             Special case text line taller than page
  650.         */
  651.  
  652.         if ((thisLine  == pageFirstLine) && (thisLineH > pageHeight)) {
  653.             do {
  654.                 pageBounds[thisPage] = pageBase+pageHeight;
  655.                 pageBase   = pageBounds[thisPage];
  656.                 thisPage  += 1;
  657.                 thisLineH -= pageHeight;
  658.             } while (thisLineH >= pageHeight);
  659.             pageSoFar = thisLineH; /* Carry bottom of large line to next page */
  660.             thisLine += 1; /* carry xs on as pageSoFar and start measuring next line */
  661.         }
  662.     }
  663.  
  664.     *nPages = thisPage;
  665. }  /* GetPageEnds */
  666.  
  667. pascal void DrawPageBreaks(DPtr theDoc)
  668. {
  669.     PageEndsArray    pageEnds;
  670.     short           nPages;
  671.     short           ctr;
  672.     short           lineBase;
  673.     short           pageHeight;
  674.     Rect            viewRect;
  675.  
  676.     pageHeight = theDoc->pageSize.bottom - theDoc->pageSize.top;
  677.  
  678.     GetPageEnds(pageHeight, theDoc->theText, pageEnds, &nPages);
  679.  
  680.     lineBase = (*(theDoc->theText))->destRect.top;
  681.     viewRect = (*(theDoc->theText))->viewRect;
  682.  
  683.     PenPat(&qd.gray);
  684.     for (ctr = 0; ctr<nPages-1; ctr++) {
  685.         MoveTo(viewRect.left, lineBase+pageEnds[ctr]);
  686.         LineTo(viewRect.right,lineBase+pageEnds[ctr]);
  687.     }
  688.     PenNormal();
  689. } /*    DrawPageBreaks */
  690.  
  691. pascal void DrawPageExtras(DPtr theDoc)
  692. {
  693.     GrafPtr       oldPort;
  694.     RgnHandle    oldClip;
  695.     Rect              rectToClip;
  696.  
  697.     GetPort(&oldPort);
  698.     SetPort(theDoc->theWindow);
  699.  
  700.     oldClip = NewRgn();
  701.     GetClip(oldClip);
  702.  
  703.     GetWinContentRect(theDoc->theWindow,&rectToClip);
  704.     ClipRect(&rectToClip);
  705.  
  706. #ifndef RUNTIME
  707.     /* draw the borders */
  708.  
  709.     if (theDoc->kind == kDocumentWindow && theDoc->u.reg.showBorders)
  710.         ShowSectionBorders(theDoc);
  711. #endif
  712.  
  713.     /* and then the page breaks */
  714.     /* DrawPageBreaks(theDoc);  Take the page breaks and shove 'em MN */
  715.  
  716.     SetClip(oldClip);
  717.  
  718.     DisposeRgn(oldClip);
  719.  
  720.     SetPort(oldPort);
  721. }  /* DrawPageExtras */
  722.  
  723. #ifndef RUNTIME
  724. #define PlotResMiniIcon(id, r)    PlotIconID(r, atNone, ttNone, id)
  725. #endif
  726.  
  727. pascal void DoUpdate(DPtr theDoc)
  728. {
  729.     WindowPtr     aWindow;
  730.     GrafPtr       savePort;
  731.     Rect             rectClip;
  732.     Rect             r;
  733.     short            icmVBase;
  734.  
  735.     aWindow = theDoc->theWindow;
  736.     GetPort(&savePort);
  737.     SetPort(aWindow);
  738.     BeginUpdate(aWindow);
  739.  
  740.     ClipRect(&aWindow->portRect);
  741.     EraseRect(&aWindow->portRect);
  742.     
  743.     icmVBase = aWindow->portRect.bottom - 13;
  744.     
  745.     SetRect(&r, 2, icmVBase, 18, icmVBase+12);
  746.     switch (theDoc->lastState & 0x000F) {
  747.     case stateConsole:
  748.         PlotResMiniIcon(ConsoleSICNID, &r);
  749.         break;
  750.     case stateDocument:
  751.         PlotResMiniIcon(DocumentSICNID, &r);
  752.         break;
  753.     }
  754.     SetRect(&r, 18, icmVBase, 34, icmVBase+12);
  755.     switch (theDoc->lastState & 0x00F0) {
  756.     case stateRdWr:
  757.         PlotResMiniIcon(EnabledSICNID, &r);
  758.         break;
  759.     case stateRdOnly:
  760.         PlotResMiniIcon(ReadOnlySICNID, &r);
  761.         break;
  762.     case stateBlocked:
  763.         PlotResMiniIcon(BlockedSICNID, &r);
  764.         break;
  765.     }
  766.     
  767.     DrawControls(aWindow);
  768.     DrawGrowIcon(aWindow);
  769.  
  770.     GetWinContentRect(aWindow, &rectClip);
  771.     ClipRect(&rectClip);
  772.  
  773.     TEUpdate(&aWindow->portRect, theDoc->theText);
  774.  
  775.     DrawPageExtras(theDoc);
  776.  
  777.     EndUpdate(aWindow);
  778.     ClipRect(&aWindow->portRect);
  779.  
  780.     SetPort(savePort);
  781. } /* DoUpdate */
  782.  
  783. #pragma segment Window
  784.  
  785. pascal DPtr NewDocument(Boolean isForOldDoc, WindowKind kind)
  786. {
  787.     short                resFile;
  788.     Rect           destRect;
  789.     Rect           viewRect;
  790.     Rect           vScrollRect;
  791.     Rect           hScrollRect;
  792.     DPtr           myDoc;
  793.     WindowPtr      myWindow;
  794.     ControlHandle  vScroll;
  795.     ControlHandle  hScroll;
  796.     Str255         theName;
  797.     Str255         newNumber;
  798. #ifndef RUNTIME
  799.     OSType            supportedInterfaces[1];
  800. #endif
  801.  
  802.     myDoc = nil;
  803.     myWindow = GetNewWindow(WindowTemplates+kind, nil, (WindowPtr)-1);
  804.  
  805.     if (!myWindow)
  806.         return nil;
  807.  
  808.     if (!isForOldDoc && kind == kDocumentWindow) {
  809.         GetWTitle(myWindow, theName);
  810.         NumToString(++gNewDocCount, newNumber);
  811.         if (gNewDocCount>1) {
  812.             PLstrcat(theName, "\p #");
  813.             PLstrcat(theName, newNumber);
  814.             SetWTitle(myWindow, theName);
  815.         }
  816.     }
  817.  
  818.     OffsetWindow(myWindow);
  819.     SetPort(myWindow);
  820.  
  821.     myDoc = (DPtr)NewPtr(sizeof(DocRec));
  822.  
  823.     SetWRefCon(myWindow, (long)myDoc);
  824.  
  825.     myDoc->theWindow = myWindow;
  826.  
  827.     vScrollRect = myWindow->portRect;
  828.  
  829.     vScrollRect.left  = vScrollRect.right - kScrollbarAdjust;
  830.     vScrollRect.right = vScrollRect.left  + kScrollbarWidth;
  831.  
  832.     vScrollRect.bottom = vScrollRect.bottom - 14;
  833.     vScrollRect.top    = vScrollRect.top - 1;
  834.  
  835.     vScroll = NewControl(myWindow, &vScrollRect, "", true, 0, 0, 0, scrollBarProc, 0);
  836.  
  837.     hScrollRect = myWindow->portRect;
  838.     hScrollRect.top = hScrollRect.bottom - kScrollbarAdjust;
  839.     hScrollRect.bottom = hScrollRect.top + kScrollbarWidth;
  840.  
  841.     hScrollRect.right = hScrollRect.right - 14;
  842.     hScrollRect.left  = hScrollRect.left + 31;
  843.     hScroll = NewControl(myWindow, &hScrollRect, "", true, 0, 0, 0, scrollBarProc, 0);
  844.  
  845.     myDoc->vScrollBar = vScroll;
  846.     myDoc->hScrollBar = hScroll;
  847.     myDoc->type            = kPlainTextDoc;
  848.     myDoc->kind         = kind;
  849.     myDoc->dirty         = false;
  850.  
  851.     if (kind == kDocumentWindow) {
  852.         myDoc->u.reg.lastID            = 0;
  853.         myDoc->u.reg.firstSection    = nil;
  854.         myDoc->u.reg.lastSection    = nil;
  855.         myDoc->u.reg.numSections    = 0;
  856.         myDoc->u.reg.everSaved     = false;
  857.         myDoc->u.reg.showBorders     = false;
  858.         
  859.         myDoc->lastState                = stateDocument + stateRdWr;
  860.     } else {
  861.         myDoc->u.cons.next            = gConsoleList;
  862.         myDoc->u.cons.cookie            = nil;
  863.         myDoc->u.cons.fence            = 0;
  864.         myDoc->u.cons.memory            = 20000;
  865.         myDoc->u.cons.selected        = false;
  866.  
  867.         gConsoleList = myDoc;
  868.  
  869.         myDoc->lastState                = stateConsole + stateBlocked;
  870.     }
  871.  
  872.     GetTERect(myWindow, &viewRect);
  873.     destRect = viewRect;
  874.  
  875.     myDoc->thePrintSetup = (THPrint)NewHandle(sizeof(TPrint));
  876.  
  877.     resFile = CurResFile();
  878.     
  879.     PrOpen();
  880.     PrintDefault(myDoc->thePrintSetup);
  881.     PrClose();
  882.     
  883.     UseResFile(resFile);
  884.  
  885.     myDoc->pageSize = (*(myDoc->thePrintSetup))->prInfo.rPage;
  886.     OffsetRect(&myDoc->pageSize, -myDoc->pageSize.left, -myDoc->pageSize.top);
  887.  
  888.     destRect.right = destRect.left + myDoc->pageSize.right;
  889.  
  890.     OffsetRect(&destRect, kTextOffset, kTextOffset);
  891.  
  892.     TextFont(gFormat.font);
  893.     TextSize(gFormat.size);
  894.     TextFace(0);
  895.  
  896.     myDoc->theText = TENew(&destRect, &viewRect);
  897.     
  898.     (*myDoc->theText)->crOnly = -1;
  899.     myDoc->theFileName[0] = 0;
  900.     myDoc->theWindow      = myWindow;
  901.  
  902. #ifndef RUNTIME
  903.     myDoc->tsmDoc                =    nil;
  904.     myDoc->tsmTERecHandle    =    nil;
  905.     
  906.     if (gTSMTEImplemented) {
  907.         supportedInterfaces[0] = kTSMTEInterfaceType;
  908.         if (NewTSMDocument(1, supportedInterfaces, &myDoc->tsmDoc,
  909.                     (long) &myDoc->tsmTERecHandle) == noErr)
  910.         {
  911.             TSMTERecPtr tsmteRecPtr = *(myDoc->tsmTERecHandle);
  912.             
  913.             tsmteRecPtr->textH = myDoc->theText;
  914.             tsmteRecPtr->preUpdateProc = nil;
  915.             tsmteRecPtr->postUpdateProc = nil;
  916.             tsmteRecPtr->updateFlag = 0;
  917.             tsmteRecPtr->refCon = (long) myDoc->theWindow;
  918.             
  919.             UseInputWindow(myDoc->tsmDoc, !gPerlPrefs.inlineInput);
  920.         } else {
  921.             myDoc->tsmDoc                =    nil;
  922.             myDoc->tsmTERecHandle    =    nil;
  923.         }
  924.     }
  925. #endif
  926.         
  927.     ResizeWindow(myDoc);
  928.     
  929.     RegisterDocument(myDoc);
  930.     
  931.     return(myDoc);
  932. }
  933.  
  934. #pragma segment Window
  935.  
  936. pascal void CloseMyWindow(WindowPtr aWindow)
  937. {
  938.     DPtr     aDocument;
  939.     TEHandle theText;
  940.  
  941.     DoHideWindow(aWindow);
  942.     aDocument = DPtrFromWindowPtr(aWindow);
  943.  
  944.     UnregisterDocument(aDocument);
  945.  
  946. #ifndef RUNTIME
  947.     if (aDocument->tsmDoc) {
  948.         FixTSMDocument(aDocument->tsmDoc);
  949.         // DeleteTSMDocument might cause crash if we don't deactivate first, so...
  950.         DeactivateTSMDocument(aDocument->tsmDoc);
  951.         DeleteTSMDocument(aDocument->tsmDoc);
  952.     }
  953. #endif
  954.  
  955.     if (aDocument->kind != kDocumentWindow) {
  956.         CloseConsole(aDocument->u.cons.cookie);
  957.         
  958.         if (gConsoleList == aDocument)
  959.             gConsoleList = aDocument->u.cons.next;
  960.         else {
  961.             DPtr doc = gConsoleList;
  962.             while (doc->u.cons.next != aDocument)
  963.                 doc = doc->u.cons.next;
  964.             doc->u.cons.next = aDocument->u.cons.next;
  965.         }
  966.     }
  967.     
  968.     theText   = aDocument->theText;
  969.     TEDispose(theText);
  970.  
  971.     if (aDocument->thePrintSetup)
  972.         DisposHandle((Handle)aDocument->thePrintSetup);
  973.  
  974.     DisposPtr((Ptr)aDocument);
  975.     DisposeWindow(aWindow);
  976.  
  977.     gWCount--;
  978. }
  979.  
  980. /*
  981.     Name     : PrintWindow
  982.     Function : Prints the document supplied in theDoc. askUser controls interaction
  983.                   with the user.
  984.  
  985.                          Uses extra memory equal to the size of the textedit use in the
  986.                          printed document.
  987. */
  988.  
  989. pascal void PrintWindow(DPtr theDoc, Boolean askUser)
  990. {
  991.     GrafPtr          oldPort;
  992.     TEHandle         printerTE;
  993.     TPPrPort         printerPort;
  994.     Rect                 printView;
  995.     PageEndsArray    pageBounds;
  996.     short              nPages;
  997.     short               pageCtr;
  998.     Boolean             abort;
  999.     short                resFile;
  1000.     Rect                 rectToClip;
  1001.     TPrStatus         thePrinterStatus;
  1002.     DialogPtr         progressDialog;
  1003.  
  1004.     abort = false;
  1005.  
  1006.     /*
  1007.         Preserve the current port
  1008.     */
  1009.     GetPort(&oldPort);
  1010.     resFile = CurResFile();
  1011.     PrOpen();
  1012.  
  1013.     if (askUser)
  1014.         if (abort = !PrJobDialog(theDoc->thePrintSetup)) {
  1015.             PrClose();
  1016.             
  1017.             goto done;
  1018.         }
  1019.  
  1020.     progressDialog = GetNewDialog(1005, nil, (WindowPtr)-1);
  1021.  
  1022.     DrawDialog(progressDialog);
  1023.  
  1024.     printerPort = PrOpenDoc(theDoc->thePrintSetup, nil, nil);
  1025.     SetPort((GrafPtr)printerPort);
  1026.  
  1027.     /*
  1028.         Put the window text into the printer port
  1029.     */
  1030.     TextFont((*theDoc->theText)->txFont);
  1031.     TextSize((*theDoc->theText)->txSize);
  1032.  
  1033.     printView = (*(theDoc->thePrintSetup))->prInfo.rPage;
  1034.     printerTE = TENew(&printView, &printView);
  1035.  
  1036.     HLock((Handle)((*(theDoc->theText))->hText));
  1037.  
  1038.     TESetText(*((*(theDoc->theText))->hText), (*(theDoc->theText))->teLength, printerTE);
  1039.  
  1040.     HUnlock((Handle)((*(theDoc->theText))->hText));
  1041.  
  1042.     /*
  1043.         Work out the offsets
  1044.     */
  1045.     (*printerTE)->destRect = printView; /* GetPageEnds calls TECalText */
  1046.  
  1047.     GetPageEnds(printView.bottom-printView.top, printerTE, pageBounds, &nPages);
  1048.  
  1049.     TEDeactivate(printerTE);
  1050.  
  1051.     for (pageCtr = 0; pageCtr <= nPages-1; pageCtr++)
  1052.         if (!abort) {
  1053.             PrOpenPage(printerPort, nil);
  1054.  
  1055.             rectToClip = printView;
  1056.  
  1057.             if (pageCtr > 0)
  1058.                 rectToClip.bottom = rectToClip.top + (pageBounds[pageCtr]-pageBounds[pageCtr-1]);
  1059.             else
  1060.                 rectToClip.bottom = rectToClip.top + pageBounds[pageCtr];
  1061.  
  1062.             ClipRect(&rectToClip);
  1063.  
  1064.             if (PrError() == iPrAbort)
  1065.                 abort = true;
  1066.  
  1067.             if (! abort)
  1068.                 TEUpdate(&printView, printerTE);
  1069.  
  1070.             if (PrError() == iPrAbort)
  1071.                 abort = true;
  1072.  
  1073.             PrClosePage(printerPort);
  1074.  
  1075.             TEScroll(0,rectToClip.top-rectToClip.bottom, printerTE);
  1076.         }
  1077.  
  1078.     TEDispose(printerTE);
  1079.     PrCloseDoc(printerPort);
  1080.  
  1081.     if (( (*(theDoc->thePrintSetup))->prJob.bJDocLoop == bSpoolLoop ) &&
  1082.             ( PrError() == noErr )  &&
  1083.             (! abort))
  1084.         PrPicFile( theDoc->thePrintSetup, nil, nil, nil, &thePrinterStatus);
  1085.  
  1086.     PrClose();
  1087.  
  1088.     DisposDialog(progressDialog);
  1089.  
  1090. done:
  1091.     SetPort(oldPort);
  1092.     UseResFile(resFile);
  1093.     InvalRect(&oldPort->portRect);
  1094. }
  1095.  
  1096. void ForceStatusRedraw(WindowPtr win)
  1097. {
  1098.     GrafPtr    oldPort;
  1099.     Rect        r;
  1100.     
  1101.     GetPort(&oldPort);
  1102.     SetPort(win);
  1103.  
  1104.     r = win->portRect;
  1105.     
  1106.     r.right = 30;
  1107.     r.top   = r.bottom - 13;
  1108.     
  1109.     InvalRect(&r);
  1110.     
  1111.     SetPort(oldPort);
  1112. }
  1113.  
  1114. pascal void ShowWindowStatus()
  1115. {
  1116.      DPtr           aDocument;
  1117.     WindowPeek    aWindow;
  1118.     WindowPeek    nextWindow;
  1119.     short            curState;
  1120.  
  1121.     for (aWindow = (WindowPeek) FrontWindow(); aWindow; aWindow = nextWindow) {
  1122.         nextWindow = aWindow->nextWindow;
  1123.         if (Ours((WindowPtr) aWindow)) {
  1124.             aDocument = DPtrFromWindowPtr((WindowPtr) aWindow);
  1125.  
  1126.             if (aDocument->kind == kDocumentWindow) 
  1127.                 curState = stateDocument + stateRdWr;
  1128.             else {
  1129.                 curState = stateConsole;
  1130.                 if (aDocument->u.cons.fence == 32767)
  1131.                     curState    += stateRdOnly;
  1132.                 else if (!gRunningPerl || !aDocument->u.cons.selected)
  1133.                     curState += stateBlocked;
  1134.                 else
  1135.                     curState += stateRdWr;
  1136.             }
  1137.             
  1138.             if (curState != aDocument->lastState) {
  1139.                 aDocument->lastState = curState;
  1140.                 ForceStatusRedraw((WindowPtr) aWindow);
  1141.             }
  1142.         }
  1143.     }
  1144. }
  1145.  
  1146. #ifndef RUNTIME
  1147. pascal void UseInlineInput(Boolean inline)
  1148. {
  1149.      DPtr           aDocument;
  1150.     WindowPeek    aWindow;
  1151.     WindowPeek    nextWindow;
  1152.  
  1153.     for (aWindow = (WindowPeek) FrontWindow(); aWindow; aWindow = nextWindow) {
  1154.         nextWindow = aWindow->nextWindow;
  1155.         if (Ours((WindowPtr) aWindow)) {
  1156.             aDocument = DPtrFromWindowPtr((WindowPtr) aWindow);
  1157.             if (aDocument->tsmDoc)
  1158.                 UseInputWindow(aDocument->tsmDoc, !inline);
  1159.         }
  1160.     }
  1161. }
  1162. #endif
  1163.  
  1164. pascal void DoShowWindow(WindowPtr win)
  1165. {
  1166.      WindowPeek    aWindow;
  1167.     WindowPeek    nextWindow;
  1168.  
  1169.     for (aWindow = (WindowPeek) FrontWindow(); aWindow; aWindow = nextWindow) {
  1170.         nextWindow = aWindow->nextWindow;
  1171.         if (Ours((WindowPtr) aWindow)) {
  1172.             goto done;
  1173.         }
  1174.     }
  1175.     
  1176.     SetLongMenus();
  1177. done:
  1178.     ShowWindow(win);
  1179. }
  1180.  
  1181. pascal void DoHideWindow(WindowPtr win)
  1182. {
  1183.      WindowPeek    aWindow;
  1184.     WindowPeek    nextWindow;
  1185.  
  1186.     HideWindow(win);
  1187.     
  1188.     for (aWindow = (WindowPeek) FrontWindow(); aWindow; aWindow = nextWindow) {
  1189.         nextWindow = aWindow->nextWindow;
  1190.         if (Ours((WindowPtr) aWindow)) {
  1191.             return;
  1192.         }
  1193.     }
  1194.     
  1195.     SetShortMenus();
  1196. }